package com.lc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lc.constant.Constants;
import com.lc.pojo.PmsSku;
import com.lc.pojo.PmsSkuPic;
import com.lc.pojo.UmsUser;
import com.lc.pojo.dto.PmsSkuCartDto;
import com.lc.pojo.dto.cartCountDto;
import com.lc.result.Result;
import com.lc.result.ResultUtils;
import com.lc.service.PmsSkuPicService;
import com.lc.utils.ThreadVariablesUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: 购物车
 * @Author lc
 */

@RestController
@RequestMapping("/oms/cart")
public class OmsCartController {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private PmsSkuPicService pspService;

    //添加购物车接口
    @PostMapping("/add")
    public Result add(Integer skuId, Integer num, HttpServletRequest request){
        String token = request.getHeader(Constants.Redis.PREFIX_TOKEN);
        if(!StringUtils.hasLength(token)){
            return ResultUtils.returnFail("添加购物车前，请登录");
        }

        //根据sku查询商品信息
        PmsSku pmsSKU = (PmsSku)redisTemplate.opsForHash().get("PmsSKU", skuId.toString());

        //存redis中
        UmsUser umsUser = ThreadVariablesUtil.getThreadData();

        PmsSkuCartDto cartDto = new PmsSkuCartDto();

        BeanUtils.copyProperties(pmsSKU,cartDto);
        cartDto.setUserId(Math.toIntExact(umsUser.getId()));
        cartDto.setSkuId(pmsSKU.getId());

        LambdaQueryWrapper<PmsSkuPic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsSkuPic::getSkuId,skuId)
                .eq(PmsSkuPic::getIsDefault,1);
        PmsSkuPic skuPic = pspService.getOne(wrapper);
        cartDto.setPicUrl(skuPic.getPicUrl());

        cartDto.setUnitPrice(pmsSKU.getPrice());
        cartDto.setNum(num);
        cartDto.setAddTime(pmsSKU.getCreatedTime());

        redisTemplate.opsForHash().put("ShopCar:"+umsUser.getAccount(),skuId.toString(),cartDto);

        if(cartDto != null){
            return ResultUtils.returnSuccess();
        }
        return ResultUtils.returnFail();
    }

    //移除购物车商品接口
    @PostMapping("/remove")
    public Result remove(String skuIds,HttpServletRequest request){
        String token = request.getHeader(Constants.Redis.PREFIX_TOKEN);
        if(!StringUtils.hasLength(token)){
            return ResultUtils.returnFail("移除购物车前，请登录");
        }

        UmsUser umsUser = ThreadVariablesUtil.getThreadData();

        List<String> skuIdsList = Arrays.asList(skuIds.split(","));
        skuIdsList.forEach(skuId->{
            redisTemplate.opsForHash().delete("ShopCar:"+umsUser.getAccount(),skuId);
        });

        return ResultUtils.returnSuccess();
    }

    //查询用户的购物车信息接口
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        String token = request.getHeader(Constants.Redis.PREFIX_TOKEN);
        if(!StringUtils.hasLength(token)){
            return ResultUtils.returnFail("查询购物车信息前，请登录");
        }
        UmsUser umsUser = ThreadVariablesUtil.getThreadData();
        List<Object> list = redisTemplate.opsForHash().values("ShopCar:" + umsUser.getAccount());

        return ResultUtils.returnDataSuccess(list);
    }

    //查询购物车中商品种类数量
    @GetMapping("/cartCount")
    public Result cartCount(HttpServletRequest request){
        String token = request.getHeader(Constants.Redis.PREFIX_TOKEN);
        if(!StringUtils.hasLength(token)){
            return ResultUtils.returnFail("查询购物车总数量前，请登录");
        }
        UmsUser umsUser = ThreadVariablesUtil.getThreadData();
        Long cartCount = redisTemplate.opsForHash().size("ShopCar:" + umsUser.getAccount());

        cartCountDto cartCountDto = new cartCountDto();
        cartCountDto.setCartCount(Math.toIntExact(cartCount));

        return ResultUtils.returnDataSuccess(cartCountDto);
    }

}
